﻿using NPOI.SS.Formula.Functions;
using NPOI.Util;
using OSSUtility.Communicate;
using OSSUtility.Device;
using Ozone.Help;
using ServiceStack.Script;
using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.TaskbarClock;

namespace Ozone.Device
{

    /// <summary>
    /// 离心机
    /// </summary>
    public class DevCentrifugalMachine : DevBase
    {
        /// <summary>
        /// 使用ModbusTCP通讯
        /// </summary>
        /// <param name="FSrvIP"></param>
        /// <param name="FPort"></param>
        public DevCentrifugalMachine(int CommType, string FSrvIP, int FPort) : base(CommType, FSrvIP, FPort)
        {

        }



        /// <summary>
        /// 打开上盖板
        /// </summary>
        /// <returns></returns>
        public bool OpenUpperplate()
        {
            bool temperature = false;
            // 00 00 00 00 00 06 01 06 20 01 00 01
            try
            {
                LogHelper.Debug("发送打开离心机指令");
                byte[] dataToSend = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x01, 0x06, 0x20, 0x01, 0x00, 0x01 };
                Thread.Sleep(1000);
                byte[] byteRcv = session_.sendAndReadByteLock(dataToSend.ToArray(), 1000);
                if (byteRcv != null && byteRcv.Length == 12)
                {
                    //发送和接受参数一直代表设置成功
                    if (dataToSend.SequenceEqual(byteRcv))
                    {
                        temperature = true;
                        LogHelper.Debug("离心机成功");
                    }
                    else
                    {
                        temperature = false;
                        LogHelper.Debug("离心机失败");

                    }
                }
            }
            catch (Exception ex)
            {

                LogHelper.Debug("异常" + ex.Message);

            }

            return temperature;

        }

        /// <summary>
        /// 关闭上盖板
        /// </summary>
        /// <returns></returns>
        public bool CloseUpperplate()
        {

            bool temperature = false;

            //00 00 00 00 00 06 01 06 20 01 00 02
            try
            {
                byte[] dataToSend = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x01, 0x06, 0x20, 0x01, 0x00, 0x02 };
                byte[] byteRcv = session_.sendAndReadByteLock(dataToSend, 2000);
                if (dataToSend.SequenceEqual(byteRcv))
                {
                    temperature = true;

                }
                else
                {
                    temperature = false;
                }
            }
            catch (Exception ex)
            {

            }

            return temperature;

        }

        /// <summary>
        /// 获取上盖状态
        /// 0-中间位置 
        ///1-开窗 2-关窗 
        ///4-中间位置光栅异物 
        ///5-开窗光栅异物 
        ///6-关窗光栅异物
        /// </summary>
        /// <returns></returns>
        public string GetUpperplateState()
        {

            string temperature = "0";


            try
            {
                byte[] dataToSend = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x22, 0x0D, 0x00, 0x01 };
                byte[] byteRcv = session_.sendAndReadByteLock(dataToSend, 1000);


                //00 00 00 00 00 05 01 03 02 00 00
                //00 00 00 00 00 05 01 03 02 00 01
                //00 00 00 00 00 05 01 03 02 00 02
                //00 00 00 00 00 05 01 03 02 00 05
                //00 00 00 00 00 05 01 03 02 00 06
                if (byteRcv != null && byteRcv.Length == 11)
                {
                    if (byteRcv[10] == 0x00)
                    {
                        temperature = "0";
                    }
                    if (byteRcv[10] == 0x01)
                    {
                        temperature = "1";
                    }
                    if (byteRcv[10] == 0x02)
                    {
                        temperature = "2";
                    }
                    if (byteRcv[10] == 0x04)
                    {
                        temperature = "4";
                    }
                    if (byteRcv[10] == 0x05)
                    {
                        temperature = "5";

                    }
                    if (byteRcv[10] == 0x06)
                    {
                        temperature = "6";

                    }
                }
            }
            catch (Exception ex)
            {

                throw;
            }

            return temperature;

        }



        /// <summary>
        /// 设置离心速度
        /// </summary>
        /// <param name="Speed"></param>
        /// <returns></returns>
        public bool SetCentrifugeSpeed(int Speed)
        {


            bool temperature = false;

            try
            {



                if (Speed > 300 && Speed < 20000)
                {


                    // 固定前缀参数
                    byte[] prefix =
                    {
            0x00, 0x00, 0x00, 0x00,
            0x00, 0x06, 0x01, 0x06,
            0x22, 0x01
            };


                    byte highByte = (byte)(Speed >> 8);   // 获取高字节
                    byte lowByte = (byte)(Speed & 0xFF);  // 获取低字节

                    // 合并前缀和数字字节
                    byte[] combined = prefix
                    .Concat(new[] { highByte, lowByte })
                    .ToArray();



                    byte[] byteRcv = session_.sendAndReadByteLock(combined.ToArray());
                    if (byteRcv != null && byteRcv.Length == 12)
                    {
                        //发送和接受参数一直代表设置成功
                        if (combined.SequenceEqual(byteRcv))
                        {
                            temperature = true;

                        }
                        else
                        {
                            temperature = false;
                        }
                    }


                }


            }
            catch (Exception ex)
            {

                temperature = false;
            }

            return temperature;

        }



        /// <summary>
        /// 设置离心机时间 单位 秒
        /// </summary>
        /// <param name="Time"></param>
        /// <returns></returns>
        public bool SetCentrifugeTime(int Time)
        {






            bool temperature = false;

            try
            {
                if (Time > 1 && Time < 59999)
                {


                    // 固定前缀参数
                    byte[] prefix =
                {
            0x00, 0x00, 0x00, 0x00,
            0x00, 0x06, 0x01, 0x06,
            0x22, 0x02
            };


                    byte highByte = (byte)(Time >> 8);   // 获取高字节
                    byte lowByte = (byte)(Time & 0xFF);  // 获取低字节

                    // 合并前缀和数字字节
                    byte[] combined = prefix
                    .Concat(new[] { highByte, lowByte })
                    .ToArray();



                    byte[] byteRcv = session_.sendAndReadByteLock(combined.ToArray());
                    if (byteRcv != null && byteRcv.Length == 12)
                    {
                        //发送和接受参数一直代表设置成功
                        if (combined.SequenceEqual(byteRcv))
                        {
                            temperature = true;

                        }
                        else
                        {
                            temperature = false;
                        }
                    }
                }


            }
            catch (Exception ex)
            {

                throw;
            }

            return temperature;

        }


        /// <summary>
        /// 设置离心机温度
        /// </summary>
        /// <param name="Temp"></param>
        /// <returns></returns>
        public bool SetCentrifugeTemp(int Temp)
        {
            bool temperature = false;

            try
            {

                if (Temp > -20 && Temp < 80)
                {
                    Temp = Temp * 10 + 500;
                    // 固定前缀参数
                    byte[] prefix =
                {
            0x00, 0x00, 0x00, 0x00,
            0x00, 0x06, 0x01, 0x06,
            0x22, 0x03
            };


                    byte highByte = (byte)(Temp >> 8);   // 获取高字节
                    byte lowByte = (byte)(Temp & 0xFF);  // 获取低字节

                    // 合并前缀和数字字节
                    byte[] combined = prefix
                    .Concat(new[] { highByte, lowByte })
                    .ToArray();



                    byte[] byteRcv = session_.sendAndReadByteLock(combined.ToArray());
                    if (byteRcv != null && byteRcv.Length == 12)
                    {
                        //发送和接受参数一直代表设置成功
                        if (combined.SequenceEqual(byteRcv))
                        {
                            temperature = true;

                        }
                        else
                        {
                            temperature = false;
                        }
                    }

                }

            }
            catch (Exception ex)
            {

                throw;
            }

            return temperature;



        }

        /// <summary>
        /// 启动离心
        /// </summary>
        /// <returns></returns>
        public bool Start()
        {

            bool temperature = false;


            try
            {
                //离心前判断仓门是否关闭
                if (GetUpperplateState() == "2")
                {
                    byte[] dataToSend = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x01, 0x06, 0x20, 0x00, 0x00, 0x01 };
                    byte[] byteRcv = session_.sendAndReadByteLock(dataToSend);
                    if (byteRcv != null)
                    {
                        temperature = true;
                    }
                }
                else
                {
                    temperature = false;
                }



            }
            catch (Exception ex)
            {

                throw;
            }

            return temperature;

        }
        /// <summary>
        /// 停止离心
        /// </summary>
        /// <param name="Speed"></param>
        /// <returns></returns>
        public bool Stop()
        {

            bool temperature = false;
            try
            {
                byte[] dataToSend = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x01, 0x06, 0x20, 0x00, 0x00, 0x00 };
                byte[] byteRcv = session_.sendAndReadByteLock(dataToSend);
                if (byteRcv != null)
                {
                    temperature = true;
                }
            }
            catch (Exception ex)
            {

                throw;
            }
            return temperature;

        }


        /// <summary>
        /// 获取运行状态  
        /// true  运行中 
        /// false  停止状态
        /// </summary>
        /// <returns></returns>
        public bool GetRunState()
        {

            bool temperature = false;

            //000000000006010322070001
            try
            {
                byte[] dataToSend = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x22, 0x07, 0x00, 0x01 };
                byte[] byteRcv = session_.sendAndReadByteLock(dataToSend);
                //00 00 00 00 00 05 01 03 02 00 00
                if (byteRcv != null && byteRcv.Length == 11)
                {
                    //转速为0 代表运行停止
                    if (byteRcv[10] == 0x00)
                    {
                        temperature = true;
                    }
                    else
                    {
                        temperature = false;

                    }
                }
            }
            catch (Exception ex)
            {

                temperature = false;
            }

            return temperature;

        }


        /// <summary>
        /// 旋转转子
        /// </summary>
        /// <param name="Number"></param>
        /// <returns></returns>
        public bool SpinXuan(int Number)
        {
            bool temperature = false;
            try
            {
                byte[] dataToSend = new byte[] { };

                //00 00 00 00 00 06 01 06 20 00 00 03
                if (Number == 1)
                {
                    dataToSend = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x01, 0x06, 0x20, 0x00, 0x00, 0x03 };


                }
                else if (Number == 2)
                {
                    dataToSend = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x01, 0x06, 0x20, 0x00, 0x00, 0x04 };

                }
                else if (Number == 3)
                {
                    dataToSend = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x01, 0x06, 0x20, 0x00, 0x00, 0x05 };

                }

                else if (Number == 4)
                {
                    dataToSend = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x01, 0x06, 0x20, 0x00, 0x00, 0x06 };

                }

                else if (Number == 5)
                {
                    dataToSend = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x01, 0x06, 0x20, 0x00, 0x00, 0x07 };

                }

                else if (Number == 6)
                {
                    dataToSend = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x01, 0x06, 0x20, 0x00, 0x00, 0x08 };

                }

                else if (Number == 7)
                {
                    dataToSend = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x01, 0x06, 0x20, 0x00, 0x00, 0x09 };

                }

                else if (Number == 8)
                {
                    dataToSend = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x01, 0x06, 0x20, 0x00, 0x00, 0x0A };

                }
                byte[] byteRcv = session_.sendAndReadByteLock(dataToSend, 500);
                if (byteRcv != null)
                {


                    LogHelper.Debug("旋转离心机转子号为：" + Number.ToString());
                    int WhileNumber = 0;
                    //判断离心机转子是否已经到达设定位置
                    while (Number != GetDevSpinXuanState())
                    {
                        //就一直查询
                        Thread.Sleep(1000);
                        WhileNumber++;
                        if (WhileNumber > 120)
                        {
                            LogHelper.Debug("旋转转子等待120S后以及没有返回正常到位信号,转子旋转失败");
                            temperature = false;
                            break;
                        }
                    }
                    temperature = true;
                }
            }
            catch (Exception ex)
            {

                throw;
            }

            return temperature;
        }


        /// <summary>
        /// 读取离心机设备转子旋转后转子到位状态
        /// </summary>
        /// <returns></returns>
        public int GetDevSpinXuanState()
        {
            int SpinNumber = 0;
            try
            {
                byte[] dataToSend = new byte[] { };
                //00 00 00 00 00 06 01 03 22 0B 00 01
                dataToSend = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x22, 0x0B, 0x00, 0x01 };
                byte[] byteRcv = session_.sendAndReadByteLock(dataToSend, 500);
                //返回值
                //00 00 00 00 00 05 01 03 02 00 00
                //00 00 00 00 00 05 01 03 02 00 03
                //00 00 00 00 00 05 01 03 02 00 04
                //00 00 00 00 00 05 01 03 02 00 05
                //00 00 00 00 00 05 01 03 02 00 07
                //00 00 00 00 00 05 01 03 02 00 08
                //00 00 00 00 00 05 01 03 02 00 09
                //00 00 00 00 00 05 01 03 02 00 0A
                //00 00 00 00 00 05 01 03 02 00 0B
                //00 00 00 00 00 05 01 03 02 00 0C
                if (byteRcv != null && byteRcv.Length == 11)
                {
                    int SpinState = Convert.ToInt32(byteRcv[10]);
                    if (SpinState == 8)
                    {
                        LogHelper.Debug("旋转工位进行中，未到达指定工位！");
                    }
                    if (SpinState == 9)
                    {
                        SpinNumber = 1;
                        LogHelper.Debug("旋转至1号工位完成");
                    }
                    if (SpinState == 10)
                    {
                        SpinNumber = 2;
                        LogHelper.Debug("旋转至2号工位完成");
                    }
                    if (SpinState == 11)
                    {
                        SpinNumber = 3;
                        LogHelper.Debug("旋转至3号工位完成");
                    }
                    if (SpinState == 12)
                    {
                        SpinNumber = 4;
                        LogHelper.Debug("旋转至4号工位完成");
                    }
                    if (SpinState == 13)
                    {
                        SpinNumber = 5;
                        LogHelper.Debug("旋转至5号工位完成");
                    }
                    if (SpinState == 14)
                    {
                        SpinNumber = 6;
                        LogHelper.Debug("旋转至6号工位完成");
                    }
                    if (SpinState == 15)
                    {
                        SpinNumber = 7;
                        LogHelper.Debug("旋转至7号工位完成");
                    }
                    if (SpinState == 16)
                    {
                        SpinNumber = 8;
                        LogHelper.Debug("旋转至8号工位完成");
                    }
                }
            }
            catch (Exception ex)
            {
                SpinNumber = 0;
            }

            return SpinNumber;
        }



    }
}
